package com.bdjk.bdmw.InfoGath.communication.server.tcp.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import org.apache.log4j.Logger;

public class TCPServer
  implements Runnable
{
  private static final Logger logger = Logger.getLogger(TCPServer.class);
  private static final String IP = "127.0.0.1";
  private static final int PORT = 4001;
  protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors() * 2;
  protected static final int BIZTHREADSIZE = 4;
  private static final EventLoopGroup bossGroup = new NioEventLoopGroup(BIZGROUPSIZE);
  private static final EventLoopGroup workerGroup = new NioEventLoopGroup(BIZTHREADSIZE);
  
  public void run()
  {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup);
    b.channel(NioServerSocketChannel.class);
    b.childHandler(new ChannelInitializer(){

		@Override
		protected void initChannel(Channel ch) throws Exception {
			// TODO Auto-generated method stub
			ChannelPipeline pipeline=ch.pipeline();
			pipeline.addLast(new TCPServerHandlerTest());
		}
    	
    });
    try
    {
      b.bind(4001).sync();
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
    logger.info("TCP服务已启动");
  }
  
  public static void shutdown()
  {
    workerGroup.shutdownGracefully();
    bossGroup.shutdownGracefully();
  }
  
  public static void main(String[] args)
    throws Exception
  {
    logger.info("准备启动TCP服务");
  }
}
